import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const Plan 					= $Lazy.state(()=>import('./plan'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
        // 重置数据
        formData:[
			{ label: '重置账期',		name:'summary_sn', full:true, type:'select',required: true },
			{ label: '重置月份',		name:'reset_date', full:true, type:'date-time',	picker: 'month', format: 3,	required: true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.props.onRef?.(this)
	}
	open = ({type,api})=>{
		const { formData } = this.state
		this.api = api
		this.type = type
		if(type===1){
			$fn.hideRows(formData,['summary_sn'], true)
			$fn.hideRows(formData,['reset_date'], false)
		}else{
			$fn.hideRows(formData,['summary_sn'], false)
			$fn.hideRows(formData,['reset_date'], true)
		}
		this.modal.open({ formRef:this.formRef })
		this.getSelect()
	}
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 限制月份
			const reset_date = $fn.getRow( formData, 'reset_date')
			const {start_time, end_time} = await f.getCache({ api:'bill_month_range' })
			reset_date.before = start_time
			reset_date.after = end_time
			
			this.setState({ formData })
		})
	}
	
	render(){
		return (
			<>
                {/* 重置数据 */}
				{FormModal.call(this,{ title:'生成/重置数据', labelWidth:'s5', width:300, 
					submit: {
						onSubmit: ()=>{
							let date
							if(this.type===1){
								const { reset_date } = this.formRef.getValue(['reset_date'])
								date = reset_date
							} else {
								const { summary_sn } = this.formRef.getValue(['summary_sn'])
								date = summary_sn
							}
							this.planRef.open({api:this.api,date,type:this.type})
							this.modal.close()
						}
					}
				})}
				<Plan onRef={ref => this.planRef = ref} />
			</>
		)
	}
}

export default Index